"""
    Author:
    Date: 2019-06-20

    微信管理后台-主播动态审核
"""

from django.shortcuts import render

from utils import ajax

from entities.consts import *
from entities import moment_mgr
from entities import user_mgr
from entities import vchat_api_mgr
from utils.log import Log

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 动态审核首页 """
    context = helpers.build_context(request)
    context['waiting_auth_moment_count'] = moment_mgr.get_moment_count_by_auth_status(AUTH_STATUS_WAITING, request)
    context['unauthorized_moment_count'] = moment_mgr.get_moment_count_by_auth_status(AUTH_STATUS_REJECTED, request)
    context['authorized_moment_count'] = moment_mgr.get_moment_count_by_auth_status(AUTH_STATUS_CERTIFIED, request)
    return render(request, 'gma/moment/moment_home.html', context)


@gma_login_required
def waiting_moments(request):
    """ 待审的动态列表 """
    context = helpers.build_context(request)
    context['moments'] = moment_mgr.get_moment_list_by_auth_status(PROFILE_AUTH_STATUS_WAITING, 100, request)
    return render(request, 'gma/moment/moment_list.html', context)


@gma_login_required
def rejected_moments(request):
    """ 审核被拒的动态列表 """
    context = helpers.build_context(request)
    context['moments'] = moment_mgr.get_moment_list_by_auth_status(AUTH_STATUS_REJECTED, 100, request)
    return render(request, 'gma/moment/moment_list.html', context)


@gma_login_required
def agreed_moments(request):
    """ 审核通过的动态列表 """
    context = helpers.build_context(request)
    context['moments'] = moment_mgr.get_moment_list_by_auth_status(AUTH_STATUS_CERTIFIED, 100, request)
    return render(request, 'gma/moment/moment_list.html', context)


@gma_login_required
def auth_moment(request, mid):
    """ 动态的审核页面 """
    moment = moment_mgr.get_moment_info(mid)

    context = helpers.build_context(request)
    context['user'] = user_mgr.get_user_detail(moment['uid'])
    context['moment'] = moment
    context['resources'] = moment_mgr.get_moment_resource(mid)
    return render(request, 'gma/moment/auth_moment.html', context)


@gma_login_required
def api_agree_moment(request):
    """ 同意动态发布 """
    mid = request.POST['moment_id'].strip()
    moment = moment_mgr.get_moment_info(mid)
    if not moment:
        return ajax.error_response('无效的动态id!')

    # 更改审核状态
    success = moment_mgr.update_moment_auth_status(mid, AUTH_STATUS_CERTIFIED)
    if not success:
        Log.error(f'Agree moment status failed: mid={mid}')
    else:
        Log.info(f'Agree moment status ok: mid={mid}')

    # # 更新用户动态
    # params = {
    #     'uid': int(moment['uid']),
    #     'mid': int(mid),
    # }
    # result = vchat_api_mgr.update_user_moment(params)
    # Log.info(f'Request api result: {result}')

    return ajax.success_response()


@gma_login_required
def api_reject_moment(request):
    """ 拒绝动态发布 """
    mid = request.POST['moment_id'].strip()
    moment = moment_mgr.get_moment_info(mid)
    if not moment:
        return ajax.error_response('无效的动态id!')

    success = moment_mgr.update_moment_auth_status(mid, AUTH_STATUS_REJECTED)
    if not success:
        Log.error(f'Reject moment status failed: mid={mid}')
    else:
        Log.info(f'Reject moment status ok: mid={mid}')

    data = {}
    return ajax.success_response(data)


if __name__ == '__main__':
    print('ok')
